package com.nhjf.service.impl.incomeAndExpenditureRecordd;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nhjf.dao.coursesSeries.CoursesSeriesDao;
import com.nhjf.dao.incomeAndExpenditureRecordd.IncomeAndExpenditureRecorddDao;
import com.nhjf.dao.oranizationAccount.OranizationAccountDao;
import com.nhjf.dao.payLog.PayLogDao;
import com.nhjf.dao.teachers.TeachersDao;
import com.nhjf.model.coursesSeries.CoursesSeries;
import com.nhjf.model.incomeAndExpenditureRecord.IncomeAndExpenditureRecordd;
import com.nhjf.model.oranizationAccount.OranizationAccount;
import com.nhjf.model.payLog.PayLog;
import com.nhjf.model.teachers.Teacher;
import com.nhjf.pojo.dto.result.ServiceResultDto;
import com.nhjf.pojo.qo.coursesSeries.CoursesSeriesQo;
import com.nhjf.pojo.qo.incomeAndExpenditureRecordd.IncomeAndExpenditureRecorddQo;
import com.nhjf.pojo.qo.oranizationAccount.OranizationAccountQo;
import com.nhjf.pojo.qo.payLog.PayLogQo;
import com.nhjf.pojo.qo.teachers.TeachersQo;
import com.nhjf.service.impl.BaseServiceImpl;
import com.nhjf.service.incomeAndExpenditureRecordd.IncomeAndExpenditureRecorddService;
import com.nhjf.service.payLog.PayLogService;
@Transactional
@Service("incomeAndExpenditureRecorddService")
public class IncomeAndExpenditureRecorddServiceImpl extends BaseServiceImpl<IncomeAndExpenditureRecordd, IncomeAndExpenditureRecorddQo,IncomeAndExpenditureRecorddDao> implements IncomeAndExpenditureRecorddService{
	@Autowired
	private IncomeAndExpenditureRecorddDao incomeAndExpenditureRecorddDao;
	@Autowired
	private OranizationAccountDao oranizationAccountDao;
	@Autowired
	private PayLogDao logDao;
	@Autowired
	private CoursesSeriesDao coursesSeriesDao;
	@Autowired
	private TeachersDao teacherDao;

	@Override
	protected IncomeAndExpenditureRecorddDao getDao() {
		// TODO Auto-generated method stub
		return incomeAndExpenditureRecorddDao;
	}
	@Override
	public ServiceResultDto capitalAddition(PayLog log) {
		// TODO Auto-generated method stub
		ServiceResultDto resultDto=new ServiceResultDto<>();
		if(log==null) {
			resultDto.setSuccess(false);
			resultDto.setResultMessage("非法操作!");
			return resultDto;
		}
		CoursesSeriesQo couQo=new CoursesSeriesQo();
		couQo.setSeachTeacher(true);
		couQo.setCoursesSeriesId(log.getCoursesSeries().getId());
		CoursesSeries series =coursesSeriesDao.queryUnique(couQo);
		OranizationAccountQo accountQo=new OranizationAccountQo();
		TeachersQo qo=new TeachersQo();
		qo.setSeachOrganization(true);
		qo.setTeacherId(series.getUploader().getId());
		Teacher teacher=teacherDao.queryUnique(qo);
		if(teacher!=null) {
			accountQo.setOrganizationId(teacher.getTraininginstitution().getId());
		}else {
			resultDto.setSuccess(false);
			resultDto.setResultMessage("账户错误");
			return resultDto;
		}
		accountQo.setSeachOranization(true);

		OranizationAccount account= oranizationAccountDao.queryUnique(accountQo);
		if(account==null) {
			account=new OranizationAccount();
			account.setAmount(0);
			account.setPendingSettlementAmount(log.getMoney());
			account.setOrganization(teacher.getTraininginstitution());
			account.setUpdateTime(new Date());
			oranizationAccountDao.save(account);
		}else {
			account.setPendingSettlementAmount(account.getPendingSettlementAmount()+log.getMoney());
			account.setUpdateTime(new Date());
			oranizationAccountDao.update(account);
		}


		
		IncomeAndExpenditureRecordd andExpenditureRecordd=new IncomeAndExpenditureRecordd();
		andExpenditureRecordd.setRemarks(log.getCoursesSeries().getCourseSecondaryTitle());
		andExpenditureRecordd.setChangeType(1);
		andExpenditureRecordd.setChangeAmount(log.getMoney());
		andExpenditureRecordd.setPayLog(log);
		andExpenditureRecordd.setCreatTime(new Date());
		andExpenditureRecordd.setIncomeAndExpenditureType(1);
		andExpenditureRecordd.setOranizationAccount(account);
		incomeAndExpenditureRecorddDao.save(andExpenditureRecordd);
		resultDto.setSuccess(true);
		resultDto.setResultMessage("创建成功");
		return resultDto;
	}

}
